﻿package com.et.ball.module {
	import flash.display.*;
	import flash.filters.DropShadowFilter;
	import flash.text.*;
	public class StyleButton extends SimpleButton 
	{
		private var _width:Number;
		private var _height:Number;
		private var _text:String;
		private var _color:uint;
		private var _upBmp:Bitmap;
		private var _overBmp:Bitmap;
		private var _downBmp:Bitmap;
		
		public function StyleButton(width:Number,height:Number,text:String,color:uint=0xcecece,upBmp:BitmapData=null,overBmp:BitmapData=null,downBmp:BitmapData=null) {
			this._width=width;
			this._height=height;
			this._text=text;
			this._color=color;
			if(upBmp){
				upState=new Bitmap(upBmp);
			}
			else
			{
				upState=createUpState();
			}
			
			if(overBmp){
				upState=new Bitmap(overBmp);
			}
			else
			{
				overState=createOverState();
			}
			
			if(downBmp){
				upState=new Bitmap(downBmp);
			}
			else
			{
				downState=createDownState();
			}
			
			
			hitTestState=upState;
		}
		private function createUpState():Sprite {
			var sprite:Sprite=new Sprite  ;
			var background:Shape=createBackground(0x2A67EE);
			var textField:TextField=createTextField(false);
			sprite.addChild(background);
			sprite.addChild(textField);
			return sprite;
		}
		private function createOverState():Sprite {
			var sprite:Sprite=new Sprite  ;
			var background:Shape=createBackground(0x3470FF);
			var textField:TextField=createTextField(false);
			sprite.addChild(background);
			sprite.addChild(textField);
			return sprite;
		}
		private function createDownState():Sprite {
			var sprite:Sprite=new Sprite  ;
			var background:Shape=createBackground(0x4D26CD);
			var textField:TextField=createTextField(true);
			sprite.addChild(background);
			sprite.addChild(textField);
			return sprite;
		}
		private function createBackground(color:uint):Shape {
			var rect:Shape=new Shape  ;
			rect.graphics.clear();
			rect.graphics.lineStyle(1,this._color);
			rect.graphics.beginFill(color);
			rect.graphics.drawRoundRect(0,0,_width,_height,15);
			rect.graphics.endFill();
			var filter:DropShadowFilter=new DropShadowFilter(2);
			rect.filters=[filter];
			return rect;
		}
		private function createTextField(downstate:Boolean):TextField {
			var textField:TextField=new TextField  ;
			textField.text=_text;
			textField.textColor=0xffffff;
			textField.width=_width;
			var format:TextFormat=new TextFormat  ;
			format.align=TextFormatAlign.CENTER;
			format.size=20;
			textField.setTextFormat(format);
			textField.y=(_height-textField.textHeight)/2;
			if (downstate) {
				textField.x+=1;
				textField.y+=1;
			}
			return textField;
		}
	}
}